<? namespace Clases;

require_once("ConexionMySQL.php");
require_once("IFas.php");
require_once("Clases/Profesor.php");


abstract class AbstractFas implements IFas
{

    public $strInsertar,$strDelete,$strUpdate,$id,$tabla;
    protected abstract function fillFas($row);
    protected abstract function comandos();
    public abstract function get($object);
    public abstract function getAll();

    function AbstractFas()
    {
        $this->comandos();
    }

    public function delete() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strDelete);
        $conexion->close();
    }

    public function getAtributosInsertar() {
        $lst = $this->getAtributos();
        $nuevoarray = $lst;
        array_pop($nuevoarray);
        return $nuevoarray;
    }

    public function insertar() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strInsertar);
        $conexion->close();
    }
    public function insertarId() {
        $conexion = new ConexionMySQL();
        $insertar = $conexion->InsertarID($this->getAtributosInsertar(), $this->strInsertar, $this->id, $this->tabla);
        $conexion->close();
        return $insertar;
    }

    public function update() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strUpdate);
        $conexion->close();
    }

    public function exec($comando)
    {
        $conexion = new ConexionMySQL();
        $exec = $conexion->GetQuery($comando);
        $conexion->close();
        return $exec;
    }
    
    public function execProcedure($procedure, $lista)
    {
        $conexion = new ConexionMySQL();
        $exec = $conexion->AccionesDB($lista, $procedure);
        $conexion->close();
        return $exec;
    }
    
    public static function getAllAbstract($tabla, $id, $objeto)
    {
        $cm = new ConexionMySQL();
        $query = "SELECT * FROM ".$tabla;
        $data = $cm->GetQuery($query);
        $lst = array();
        $cm->close();
        try {
            while ($row = mysql_fetch_row($data)) {
                $abstract = AbstractFas::getAbstractFas($objeto,$row[0],$tabla,$id);
                array_push($lst,$abstract);
            }

        } catch (Exception $ex) {

        }
        
        return $lst;
    }
    
    public static function getAbstractFasC($tabla, $id, $value, $id2,$value2) 
    {    
        try{
            $conexion = new ConexionMySQL();
            $query = "SELECT * FROM ".$tabla." where ".$id." = '" . $value . "' AND ".$id2." = ".$value2;
            $rs = $conexion->GetQuery($query);
            if($tabla == "`ExamenAlumno`")
                $abstract = new \ExamenAlumno();
            return $abstract;
        }
        catch(Exception $e){return null;}
    }
    
    public static function getAbstractFas($abstract, $value, $tabla, $id) 
    {    
        try{
            $conexion = new ConexionMySQL();
            $query = "SELECT * FROM ".$tabla." where ".$id." = '" . $value . "'";
            
            $rs = $conexion->GetQuery($query);
            if($tabla == "`Profesor`")
                $abstract = new \Profesor();
            else if($tabla == "`opcion`")
                $abstract = new \Opcion();
            else if($tabla == "`problema`")
                $abstract = new \Problema();
            if($tabla == "`ExamenAlumno`")
                $abstract = new \ExamenAlumno();
            $abstract->fillFas($rs);
            return $abstract;
        }
        catch(Exception $e){return null;}
    }
    
}
?>